package com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.service.impl;

import com.jxdinfo.hussar.cloud.module.example.tjjc.towercrane.dao.TowerCraneMapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towercrane.model.TowerCrane;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.vo.AppTowerNoticeDetailVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.vo.TowerNoticeSearchVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.vo.TowerNoticeVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.dao.TowerNoticePersonMapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.model.TowerNoticePerson;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateUtil;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.model.TowerNotice;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.service.TowerNoticeService;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.dao.TowerNoticeMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernotice.vo.TowerNoticePageVO;

import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;

/**
 * 塔机异常通知服务实现类
 */
@Service("tjjc.towernotice.TowerNoticeServiceImpl")
@HussarDs("master")
public class TowerNoticeServiceImpl extends HussarServiceImpl<TowerNoticeMapper, TowerNotice> implements TowerNoticeService {

    private static final Logger logger = LoggerFactory.getLogger(TowerNoticeServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private TowerNoticeMapper towerNoticeMapper;
    @Autowired
    private TowerNoticePersonMapper towerNoticePersonMapper;
    @Autowired
    private TowerCraneMapper towerCraneMapper;

    @Override
    public ApiResponse<TowerNoticePageVO> findByPage(TowerNoticeSearchVO towerNoticeSearchVO) {
        QueryWrapper<TowerNotice> wrapper = new QueryWrapper();
        Page<TowerNotice> page = new Page<>();
        page.setPages(towerNoticeSearchVO.getPage());
        page.setSize(towerNoticeSearchVO.getRows());

        wrapper.eq("notice_status", 1);
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTowerId())) {
            wrapper.eq("tower_id", towerNoticeSearchVO.getTowerId());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeLevel())) {
            wrapper.eq("notice_level", towerNoticeSearchVO.getNoticeLevel());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeType())) {
            wrapper.eq("notice_type", towerNoticeSearchVO.getNoticeType());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", towerNoticeSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeStart())) {
            wrapper.ge("send_time", towerNoticeSearchVO.getTyCreateTimeStart());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeEnd())) {
            wrapper.le("send_time", towerNoticeSearchVO.getTyCreateTimeEnd());
        }

        wrapper.orderByDesc("send_time");

        Page<TowerNotice> noticePage = towerNoticeMapper.selectPage(page, wrapper);
        TowerNoticePageVO towerNoticePageVO = new TowerNoticePageVO();
        towerNoticePageVO.setCount(noticePage.getTotal());
        towerNoticePageVO.setData(noticePage.getRecords());
        towerNoticePageVO.setCode(RETURN_CODE);

        return ApiResponse.success(towerNoticePageVO);
    }

    @Override
    public ApiResponse<List<TowerNoticeVO>> findByList(TowerNoticeSearchVO towerNoticeSearchVO) {
        QueryWrapper<TowerNotice> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTowerId())) {
            wrapper.eq("tower_id", towerNoticeSearchVO.getTowerId());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeLevel())) {
            wrapper.eq("notice_level", towerNoticeSearchVO.getNoticeLevel());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeType())) {
            wrapper.eq("notice_type", towerNoticeSearchVO.getNoticeType());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", towerNoticeSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeStart())) {
            wrapper.ge("ty_create_time", towerNoticeSearchVO.getTyCreateTimeStart());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeEnd())) {
            wrapper.le("ty_create_time", towerNoticeSearchVO.getTyCreateTimeEnd());
        }

        wrapper.orderByDesc("ty_create_time");

        List<TowerNotice> list = towerNoticeMapper.selectList(wrapper);

        List<TowerNoticeVO> towerNoticeVOS = BeanUtil.copyToList(list, TowerNoticeVO.class);

        return ApiResponse.success(towerNoticeVOS);
    }

    @Override
    public ApiResponse<TowerNoticePageVO> findMobileByPage(TowerNoticeSearchVO towerNoticeSearchVO) {
        QueryWrapper<TowerNotice> wrapper = new QueryWrapper();
        Page<TowerNotice> page = new Page<>();
        page.setPages(towerNoticeSearchVO.getPage());
        page.setSize(towerNoticeSearchVO.getRows());

        wrapper.eq("notice_status", 1);
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTowerId())) {
            wrapper.eq("tower_id", towerNoticeSearchVO.getTowerId());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeLevel())) {
            wrapper.eq("notice_level", towerNoticeSearchVO.getNoticeLevel());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getNoticeType())) {
            wrapper.eq("notice_type", towerNoticeSearchVO.getNoticeType());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", towerNoticeSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeStart())) {
            wrapper.ge("send_time", towerNoticeSearchVO.getTyCreateTimeStart());
        }
        if(ObjectUtils.isNotEmpty(towerNoticeSearchVO.getTyCreateTimeEnd())) {
            wrapper.le("send_time", towerNoticeSearchVO.getTyCreateTimeEnd());
        }

        wrapper.orderByDesc("read_status");
        wrapper.orderByDesc("send_time");

        Page<TowerNotice> towerNoticePage = towerNoticeMapper.selectPage(page, wrapper);
        TowerNoticePageVO towerNoticePageVO = new TowerNoticePageVO();
        towerNoticePageVO.setCount(towerNoticePage.getTotal());
        towerNoticePageVO.setData(towerNoticePage.getRecords());
        towerNoticePageVO.setCode(RETURN_CODE);

        if(towerNoticePageVO.getCount() > 0){
            for(TowerNotice  towerNotice: towerNoticePageVO.getData()){
                //获取当前登录人消息已读未读记录
                QueryWrapper<TowerNoticePerson> personWrapper = new QueryWrapper<>();
                personWrapper.eq("notice_id", towerNotice.getId());
                personWrapper.eq("person_id", "ContextUtil.getUserId()");
                TowerNoticePerson  towerNoticePerson = towerNoticePersonMapper.selectOne(personWrapper);
                if(ObjectUtils.isNotEmpty(towerNoticePerson)){
                    towerNotice.setReadStatus(towerNoticePerson.getReadStatus());
                }else{
                    towerNotice.setReadStatus(1);
                }
            }
        }

        return ApiResponse.success(towerNoticePageVO);
    }

    @Override
    public ApiResponse<AppTowerNoticeDetailVO> findTowerNoticeDetail(String noticeId, String tyProjectCode) {
        QueryWrapper<TowerNotice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", noticeId);
        wrapper.eq("ty_project_code", tyProjectCode);
        TowerNotice towerNotice = towerNoticeMapper.selectOne(wrapper);
        if(ObjectUtils.isNotEmpty(towerNotice)){

            //更新消息已读未读状态
            int count = towerNoticePersonMapper.updateNoticeReadStatus(noticeId, "ContextUtil.getUserId()");
            if(count > 0){
                towerNoticeMapper.updateReadStatus(noticeId);
            }

            AppTowerNoticeDetailVO appTowerNoticeDetailDto = new AppTowerNoticeDetailVO();

            //获取塔机信息
            QueryWrapper<TowerCrane> towerCraneWrapper = new QueryWrapper<>();
            towerCraneWrapper.eq("id", towerNotice.getTowerId());
            towerCraneWrapper.eq("ty_project_code", tyProjectCode);
            towerCraneWrapper.eq("ty_delete_status", 0);
            TowerCrane towerCrane = towerCraneMapper.selectOne(towerCraneWrapper);

            appTowerNoticeDetailDto.setTowerName(towerNotice.getTowerName());
            appTowerNoticeDetailDto.setNoticeContent(towerNotice.getContent());
            appTowerNoticeDetailDto.setNoticeTime(DateUtil.toDate(towerNotice.getSendTime()));
            appTowerNoticeDetailDto.setNoticePerson(towerNotice.getNoticePerson());
            appTowerNoticeDetailDto.setMeasureValue(towerNotice.getMeasureValue().toString());

            //判断告警状态
            if(towerNotice.getNoticeLevel()==1){
                appTowerNoticeDetailDto.setMonitorStatus("预警");
            }else if(towerNotice.getNoticeLevel()==2){
                appTowerNoticeDetailDto.setMonitorStatus("报警");
            }else if(towerNotice.getNoticeLevel()==3){
                appTowerNoticeDetailDto.setMonitorStatus("设备故障");
            }

            //判断监测项
            //1回转角度 2幅度 3风速 4速度 5吊重 6吊钩高度 7X轴倾斜角度 8Y轴倾斜角度 9力矩
            if(towerNotice.getNoticeType()==1){
                appTowerNoticeDetailDto.setMonitorItem("回转角度");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnAngleValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnAngleValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnAngleRule())){
                        if(towerCrane.getWarnAngleRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnAngleRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnAngleRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnAngleRule()==4){
                            appTowerNoticeDetailDto.setWarningRule(">=");
                        }else if(towerCrane.getWarnAngleRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorAngleValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorAngleValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorAngleRule())){
                        if(towerCrane.getErrorAngleRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorAngleRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorAngleRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorAngleRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorAngleRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==2){
                appTowerNoticeDetailDto.setMonitorItem("幅度");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnRadiusValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnRadiusValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnRadiusRule())){
                        if(towerCrane.getWarnRadiusRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnRadiusRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnRadiusRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnRadiusRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getWarnRadiusRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorRadiusValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorRadiusValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorRadiusRule())){
                        if(towerCrane.getErrorRadiusRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorRadiusRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorRadiusRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorRadiusRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorRadiusRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==3){
                appTowerNoticeDetailDto.setMonitorItem("风速");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnWindValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnWindValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnWindRule())){
                        if(towerCrane.getWarnWindRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnWindRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnWindRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnWindRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getWarnWindRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorWindValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorWindValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorWindRule())){
                        if(towerCrane.getErrorWindRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorWindRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorWindRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorWindRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorWindRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==4){
                appTowerNoticeDetailDto.setMonitorItem("速度");
            }else if(towerNotice.getNoticeType()==5){
                appTowerNoticeDetailDto.setMonitorItem("吊重");
                if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorLoadValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorLoadValue());
                    appTowerNoticeDetailDto.setAlarmRule(">=");
                }
            }else if(towerNotice.getNoticeType()==6){
                appTowerNoticeDetailDto.setMonitorItem("吊钩高度");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnHookHeightValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnHookHeightValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnHookHeightRule())){
                        if(towerCrane.getWarnHookHeightRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnHookHeightRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnHookHeightRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnHookHeightRule()==4){
                            appTowerNoticeDetailDto.setWarningRule(">=");
                        }else if(towerCrane.getWarnHookHeightRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorHookHeightValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorHookHeightValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorHookHeightRule())){
                        if(towerCrane.getErrorHookHeightRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorHookHeightRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorHookHeightRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorHookHeightRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorHookHeightRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==7){
                appTowerNoticeDetailDto.setMonitorItem("X轴倾斜角度");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnXValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnXValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnXRule())){
                        if(towerCrane.getWarnXRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnXRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnXRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnXRule()==4){
                            appTowerNoticeDetailDto.setWarningRule(">=");
                        }else if(towerCrane.getWarnXRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorXValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorXValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorXRule())){
                        if(towerCrane.getErrorXRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorXRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorXRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorXRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorXRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==8){
                appTowerNoticeDetailDto.setMonitorItem("Y轴倾斜角度");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnYValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnYValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnYRule())){
                        if(towerCrane.getWarnYRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnYRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnYRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnYRule()==4){
                            appTowerNoticeDetailDto.setWarningRule(">=");
                        }else if(towerCrane.getWarnYRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorYValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorYValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorYRule())){
                        if(towerCrane.getErrorYRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorYRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorYRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorYRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorYRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }else if(towerNotice.getNoticeType()==9){
                appTowerNoticeDetailDto.setMonitorItem("力矩");
                if(towerNotice.getNoticeLevel()==1 && ObjectUtils.isNotEmpty(towerCrane.getWarnTorqueValue())){
                    appTowerNoticeDetailDto.setWarningValue(towerCrane.getWarnTorqueValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getWarnTorqueRule())){
                        if(towerCrane.getWarnTorqueRule()==1){
                            appTowerNoticeDetailDto.setWarningRule("<");
                        }else if(towerCrane.getWarnTorqueRule()==2){
                            appTowerNoticeDetailDto.setWarningRule("<=");
                        }else if(towerCrane.getWarnTorqueRule()==3){
                            appTowerNoticeDetailDto.setWarningRule(">");
                        }else if(towerCrane.getWarnTorqueRule()==4){
                            appTowerNoticeDetailDto.setWarningRule(">=");
                        }else if(towerCrane.getWarnTorqueRule()==5){
                            appTowerNoticeDetailDto.setWarningRule("=");
                        }
                    }
                }else if(towerNotice.getNoticeLevel()==2 && ObjectUtils.isNotEmpty(towerCrane.getErrorTorqueValue())){
                    appTowerNoticeDetailDto.setAlarmValue(towerCrane.getErrorTorqueValue());
                    if(ObjectUtils.isNotEmpty(towerCrane.getErrorTorqueRule())){
                        if(towerCrane.getErrorTorqueRule()==1){
                            appTowerNoticeDetailDto.setAlarmRule("<");
                        }else if(towerCrane.getErrorTorqueRule()==2){
                            appTowerNoticeDetailDto.setAlarmRule("<=");
                        }else if(towerCrane.getErrorTorqueRule()==3){
                            appTowerNoticeDetailDto.setAlarmRule(">");
                        }else if(towerCrane.getErrorTorqueRule()==4){
                            appTowerNoticeDetailDto.setAlarmRule(">=");
                        }else if(towerCrane.getErrorTorqueRule()==5){
                            appTowerNoticeDetailDto.setAlarmRule("=");
                        }
                    }
                }
            }
            return ApiResponse.success(appTowerNoticeDetailDto);
        }
        return null;
    }

    @Override
    public ApiResponse<String> add(TowerNoticeSearchVO towerNoticeSearchVO) {
        TowerNotice towerNotice = new TowerNotice();
        BeanUtil.copyProperties(towerNoticeSearchVO, towerNotice);
        towerNotice.setTyProjectCode("10101");//todo
        towerNotice.setTyCreateTime(DateUtil.toLocalDateTime(new Date()));
        towerNotice.setTyCreateUserid("ContextUtil.getUserId()");//todo
        towerNotice.setTyCreateUserName("ContextUtil.getUserName()");//todo

        towerNoticeMapper.insert(towerNotice);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> updateTowerNotice(TowerNoticeSearchVO towerNoticeSearchVO) {
        TowerNotice towerNoticeOld = towerNoticeMapper.selectById(towerNoticeSearchVO.getId());

        if(towerNoticeOld != null){

            BeanUtil.copyProperties(towerNoticeSearchVO, towerNoticeOld);
            towerNoticeOld.setTyLastUpdateTime(DateUtil.toLocalDateTime(new Date()));
            towerNoticeOld.setTyLastUpdateUserid("ContextUtil.getUserId()");//todo
            towerNoticeOld.setTyLastUpdateUserName("ContextUtil.getUserName()");//todo

            towerNoticeMapper.updateById(towerNoticeOld);
        }

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> updateAllNoticeReadStatus(String tyProjectCode) {
        //获取当前登录用户的所有未读消息
        QueryWrapper<TowerNoticePerson> wrapper = new QueryWrapper();
        wrapper.eq("person_id", "ContextUtil.getUserId()");//todo
        wrapper.eq("read_status", 0);
        wrapper.eq("ty_project_code", tyProjectCode);
        List<TowerNoticePerson> towerNoticePersonList = towerNoticePersonMapper.selectList(wrapper);

        if(ObjectUtils.isNotEmpty(towerNoticePersonList)){
            //更新已读状态
            towerNoticePersonMapper.updateAllNoticeReadStatus("ContextUtil.getUserId()");

            for(TowerNoticePerson towerNoticePerson:towerNoticePersonList){
                String noticeId = towerNoticePerson.getNoticeId();
                towerNoticeMapper.updateReadStatus(noticeId);
            }
        }

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> deleteById(TowerNoticeSearchVO towerNoticeSearchVO) {
        TowerNotice towerNoticeOld = towerNoticeMapper.selectById(towerNoticeSearchVO.getId());

        if(towerNoticeOld != null){

            //todo 原代码为逻辑删除
            towerNoticeMapper.deleteById(towerNoticeOld.getId());
        }

        return ApiResponse.success();
    }
}
